<template>
  <div
    class="left"
    :style="{'height':win_size.height,'width':$store.state.leftmenu.width}"
    id='admin-left'>
    <div
      id='left-menu'
      :class="['custom-scrollbar', {'hover' : scrollbarVisible}]"
      @scroll="paperScroll">
      <el-row class='tac'
              v-for="(route, index) in $router.options.routes"
              :key="index">
        <el-col :span="24">
          <el-menu
            :default-active="$route.path"
            :collapse="isCollapse"
            unique-opened
            router>
            <template v-for="(item, subindex) in route.children">
              <!-- 判断时候为只有一级目录 -->
              <template v-if="!item.is_once">
                <el-submenu :index="item.path" :key="subindex">
                  <template slot="title">
                    <i class="iconfont" :class="[item.icon]"></i>
                    <span slot="title">{{item.label}}</span>
                  </template>
                  <el-menu-item
                    v-for='(child, cindex) in item.children'
                    v-if="!child.is_menu_hide"
                    :key="cindex"
                    :style="{'padding-left': $store.state.leftmenu.menu_flag? '40px' : '20px'}"
                    :index="$store.state.router.headerCurRouter + '/' + item.path + '/' + child.path">
                    {{child.label}}
                  </el-menu-item>
                </el-submenu>
              </template>
              <template v-else>
                <el-menu-item :index="$store.state.router.headerCurRouter + '/' + item.path + '/' +item.children[0].path" :key="subindex">
                  <i :class="['iconfont', item.icon]"></i>
                  <span slot="title">{{item.label}}</span>
                </el-menu-item>
              </template>
            </template>
          </el-menu>
        </el-col>
      </el-row>
    </div>
    <div class="toggle-menu"
         @click='toggleMenu'
         :style="{'width': $store.state.leftmenu.width}">
      <i :class="[isCollapse ? 'el-icon-arrow-right' : 'el-icon-arrow-left']"></i>
    </div>
  </div>
</template>

<script>
import scrollbar from 'mixins/scrollbar';

export default {
  name: 'left-menu',
  mixins: [scrollbar],
  data() {
    return {
      menu_list: [],
      win_size: {
        height: ''
      },
      isCollapse: false,
      scrollbarVisible: true
    };
  },
  methods: {
    setSize() {
      this.win_size.height = this.$_$(window).height() + 'px';
    },
    toggleMenu() {
      this.isCollapse = !this.isCollapse;
      this.$store.dispatch(
        this.$store.state.leftmenu.menu_flag
          ? 'set_menu_close'
          : 'set_menu_open'
      );
    },
    updateCurMenu(route) {
      route = route || this.$route;
      if (route.matched.length) {
        var rootPath = route.matched[0].path;
        var fullPath = route.path;
        this.$store.dispatch('set_cur_route', {
          rootPath,
          fullPath
        });
        var routes = this.$router.options.routes;
        for (var i = 0; i < routes.length; i++) {
          if (routes[i].path === rootPath && !routes[i].hidden) {
            this.menu_list = routes[i].children;
            break;
          }
        }
      } else {
        this.$router.push('/404');
      }
    }
  },

  created() {
    this.setSize();

    this.$_$(window).resize(() => {
      this.setSize();
    });
    this.updateCurMenu();
  },

  mounted() {

  },
  watch: {
    $route(to, from) {
      this.updateCurMenu(to);
    }
  }
};
</script>

<style scoped lang='scss'>
// 左边导航折叠样式
#admin-left {
  position: fixed;
  float: left;
  top: 60px;
  padding-bottom: 110px;
  min-height: 100%;
  background-color: var(--menu-bgcolor);
  #left-menu {
    height: 100%;
    overflow-y: auto;
    overflow-x: hidden;
  }
  .el-menu {
    background-color: var(--menu-bgcolor);
    border-right: 0;
  }

  // 伸缩按钮样式
  .toggle-menu {
    height: 50px;
    background: #1f2f3d;
    position: fixed;
    bottom: 0px;
    left: 0px;
    z-index: 1000;
    cursor: pointer;
    line-height: 50px;
    text-align: center;
    color: #fff;
    font-size: 14px;
    .el-row {
      margin-bottom: 60px;
    }
  }
}
</style>

<style lang='scss'>
/* 左边导航样式 */
.el-submenu__title:hover,
.el-menu-item:hover,
.el-menu-item:focus {
  background-color: #d3dce6;
}
.el-menu.el-menu--inline {
  background-color: var(--submenu-bgcolor);
}
.el-menu--vertical {
  max-height: 100%;
  overflow: auto;
  &::-webkit-scrollbar {
    visibility: hidden;
  }
}
</style>
